/*
 * The National Parks Database Project
 * Group: Tyler Simrell, Vladmir Dudnic, Michael Jagielo
 * AddPanel.java
 */

package panels;

import gui.MainFrame;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import database.DBInterface;

/**
 * This JPanel class creates the panel the houses the the fields necessary for the user to add a trail
 * to the database.
 * @author Tyler Simrell
 * @version 5/24/13
 */
@SuppressWarnings("serial")
public class AddPanel extends JPanel implements ActionListener
{
	private CardLayout cards;
	private JPanel cardPanel;
	JTextField parkNameField;
	JTextField vistaPointField;
	JTextField slopeField;
	JTextField lengthField;
	JTextField trailNameField;
	JTextField washoutTextField;
	JTextField meltoutTextField;
	JComboBox<String> terrainComboBox;
	JComboBox<String> difficultyComboBox;
	
	/**
	 * This constructor takes in a reference to the card layout and the JPanel that houses the card layout.
	 * @param passedInCards The card layout for all of the panels.
	 * @param passedInPanel The JPanel that the card layout is on top of.
	 */
	public AddPanel(CardLayout passedInCards, JPanel passedInPanel)
	{
		super();
		cards = passedInCards;
		cardPanel = passedInPanel;
		setUp();
	}
	
	/**
	 * This method creates much the UI for this panel.
	 */
	private void setUp()
	{
		cards.addLayoutComponent(this, MainFrame.ADD_DB_PANEL);
		this.setLayout(null);
		
		JLabel parkNameLabel = new JLabel("Park Name");
		parkNameLabel.setBounds(82, 164, 69, 16);
		this.add(parkNameLabel);
		
		parkNameField = new JTextField();
		parkNameField.setBounds(205, 162, 388, 20);
		this.add(parkNameField);
		parkNameField.setColumns(10);
		
		JLabel vistaPointLabel = new JLabel("Vista Points");
		vistaPointLabel.setBounds(82, 205, 69, 16);
		this.add(vistaPointLabel);
		
		vistaPointField = new JTextField();
		vistaPointField.setBounds(205, 203, 211, 20);
		this.add(vistaPointField);
		vistaPointField.setColumns(10);
		
		JLabel slopeLabel = new JLabel("Slope");
		slopeLabel.setBounds(82, 243, 55, 16);
		this.add(slopeLabel);
		
		slopeField = new JTextField();
		slopeField.setBounds(205, 241, 69, 20);
		this.add(slopeField);
		slopeField.setColumns(10);
		
		JLabel lengthLabel = new JLabel("Length");
		lengthLabel.setBounds(82, 275, 55, 16);
		this.add(lengthLabel);
		
		lengthField = new JTextField();
		lengthField.setBounds(205, 275, 69, 20);
		this.add(lengthField);
		lengthField.setColumns(10);
		
		JLabel terrainLabel = new JLabel("Terrain");
		terrainLabel.setBounds(82, 314, 55, 16);
		this.add(terrainLabel);
		
		terrainComboBox = new JComboBox<String>();
		terrainComboBox.setBounds(205, 312, 81, 20);
		terrainComboBox.setToolTipText("The type of terrain that the trail has.");
		terrainComboBox.setModel(new DefaultComboBoxModel<String>(MainFrame.TERRAIN_TYPES));
		this.add(terrainComboBox);
		
		JLabel difficultyLabel = new JLabel("Difficulty");
		difficultyLabel.setBounds(398, 315, 55, 16);
		this.add(difficultyLabel);
		
		difficultyComboBox = new JComboBox<String>();
		difficultyComboBox.setModel(new DefaultComboBoxModel<String>(MainFrame.DIFFICULTY_LEVELS));
		difficultyComboBox.setToolTipText("The overall difficulty of the trail, which is derived from the length, slope, and terrain.");
		difficultyComboBox.setBounds(524, 313, 81, 20);
		this.add(difficultyComboBox);
		
		JLabel newTrailLabel = new JLabel("Add New Trail");
		newTrailLabel.setFont(new Font("Tempus Sans ITC", Font.BOLD, 17));
		newTrailLabel.setBounds(361, 12, 132, 16);
		this.add(newTrailLabel);
		
		JLabel trailNameLabel = new JLabel("Trail Name");
		trailNameLabel.setBounds(82, 123, 69, 16);
		this.add(trailNameLabel);
		
		trailNameField = new JTextField();
		trailNameField.setBounds(205, 121, 388, 20);
		this.add(trailNameField);
		trailNameField.setColumns(10);
		
		JLabel instructionsLabel = new JLabel("Please Enter The Information Relating to the Trail");
		instructionsLabel.setFont(new Font("Times New Roman", Font.BOLD, 18));
		instructionsLabel.setBounds(227, 70, 391, 16);
		this.add(instructionsLabel);
		
		JLabel washoutLabel = new JLabel("Washout %");
		washoutLabel.setBounds(398, 243, 69, 16);
		this.add(washoutLabel);
		
		washoutTextField = new JTextField();
		washoutTextField.setBounds(524, 241, 69, 20);
		this.add(washoutTextField);
		washoutTextField.setColumns(10);
		
		JLabel brushoutLabel = new JLabel("Meltout %");
		brushoutLabel.setBounds(398, 275, 69, 16);
		this.add(brushoutLabel);
		
		meltoutTextField = new JTextField();
		meltoutTextField.setBounds(523, 273, 69, 20);
		this.add(meltoutTextField);
		meltoutTextField.setColumns(10);
		
		JButton submitButton = new JButton("Submit");
		submitButton.setBounds(355, 353, 98, 26);
		submitButton.addActionListener(this);
		this.add(submitButton);
		
		this.add(new LowerPanel(cards, cardPanel, true));
	}

	/**
	 * This actionPeformed method bundles the relevant information and sends it to the DBInterface to
	 * be processed by the database.
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(parkNameField.getText().isEmpty() ||
		   slopeField.getText().isEmpty() ||
		   lengthField.getText().isEmpty() ||
		   trailNameField.getText().isEmpty() ||
		   washoutTextField.getText().isEmpty() ||
		   meltoutTextField.getText().isEmpty())
		{
			JOptionPane.showMessageDialog(null, "A Field (besides Vista) Was Left Empty", 
					"Trail Information Error", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			try
			{
				double slope = Double.parseDouble(slopeField.getText());
				double length = Double.parseDouble(lengthField.getText());
				double washout = Double.parseDouble(washoutTextField.getText());
				double meltout = Double.parseDouble(meltoutTextField.getText());
				
				String isOpen = null;
				if(meltout > 60 || washout > 60)
				{
					isOpen = "Closed";
				}
				else
				{
					isOpen = "Open";
				}
				
				DBInterface db = new DBInterface();
				db.addToInventoryTable(trailNameField.getText(), parkNameField.getText(), 
						vistaPointField.getText(), slope, length, 
						terrainComboBox.getSelectedItem().toString(), 
						difficultyComboBox.getSelectedItem().toString());
				db.addToMaintenanceTable(trailNameField.getText(), meltout, washout, isOpen);
				cards.show(cardPanel, MainFrame.MAINTENANCE_PANEL);
			}
			catch(NumberFormatException exception)
			{
				JOptionPane.showMessageDialog(null, "A Field Had a non number when it was " +
						"expecting a number", "Trail Information Error", 
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
